<?php
/**
 * 首页微博分享推荐
 * 
 * @copyright dongping
 * @since 2012-04-09
 */

error_reporting(0);

/* 引入相关文件 */
include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/config/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/foodv2/inc.php");

// 所有的参数
$g_cgival = array();
// 存储中间数据 非输出数据 可以不用
$g_pro    = array();
// 要显示的数据 输出数据
$g_show   = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	
	cgi::both($g_cgival["id"], "id", 0);
	$g_cgival["id"] = intval($g_cgival["id"]);

	
	cgi::both($g_cgival["link"], "link", "");
	$g_cgival["link"] = trim($g_cgival["link"]);
	$g_cgival["link"] = string::un_script_code($g_cgival["link"]);
	$g_cgival["link"] = string::un_html($g_cgival["link"]);
	if (strlen($g_cgival["link"]) < 1)
	{
		throw new Exception("链接地址必需填写");
	}
	if (!string::is_url($g_cgival["link"]))
	{
		throw new Exception("非法链接地址！！！");
	}
	
	
	cgi::both($g_cgival["pos"], "pos", 0);
	$g_cgival["pos"] = intval($g_cgival["pos"]);
	if ($g_cgival["pos"] < 0)
	{
		throw new Exception("推荐位置必须选择一个");
	}
	
	cgi::both($g_cgival["desc"], "desc", "");
	$g_cgival["desc"] = string::un_script_code($g_cgival["desc"]);
	$g_cgival["desc"] = string::un_html($g_cgival["desc"]);
	if (strlen($g_cgival["desc"]) < 1 or strlen(iconv("utf-8", "gbk", $g_cgival["desc"])) > 24)
	{
		throw new Exception("描述信息必需填写，并且在12个汉字以内");
	}	
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	
	$g_show = $g_cgival;
	$id = $g_cgival["id"];
	$link = $g_cgival["link"];
	$pos = $g_cgival["pos"];
	$desc = $g_cgival["desc"];
	$img_src = "";
	
	

	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();
	$admin_name = admin::admin_get_login_user_name();	
	$admin_log = new admin_log();
	
	if ($_FILES['file1'] and $_FILES["file1"]["tmp_name"] != "")
	{
	
		try
		{
			/* 上传图片 */
			$upload = new s3_upload();
			$filename = "/index_share/".time();
			if ($pos == 1 or $pos == 2 or $pos == 3 or $pos == 4)
			{
				$zomm = "180,160";
			}
			else if ($pos == 5)
			{
				$zomm = "361,160";
			}
			else if ($pos == 6 or $pos == 0)
			{
				$zomm = "195,321";
			}
			
			$data = $upload->show($_FILES['file1'])->zomm($zomm)->save($filename);
			
			if (is_array($data))
			{
				$img_src = $data["src"];
			}
		}
		catch (Exception $e)
		{
			throw new Exception($e->getMessage(), 1);
		}
	}
		
	
	
	$db = new db_food_index_share_tj();
	$db->debug = true;

	if ($id > 0)
	{
		// 编辑功能
		
		$ret = $db->update_share_item($admin_id, $admin_name, $id, $img_src, $link, $pos, $desc);
		if ($ret === false)
		{
			throw new Exception("抱歉，更新分享时出错！ " . $id);
		}		
		
		$admin_log->admin_insert_log_info($admin_id, $site_id, 6, "编辑 首页美食分享id={$id}");
	}
	else
	{
		// 增加功能
		$pos_have_data = $db->get_share_list_by_pos($site_id, $pos);
		if ($pos_have_data != false and count($pos_have_data) > 0)
		{
			throw new Exception("抱歉，已经添加过的位置不能再添加。 ");
		}
		
		$ret = $db->insert_share_item($site_id, $img_src, $link, $pos, $desc, $admin_id, $admin_name);
		if ($ret == false)
		{		
			throw new Exception("抱歉，添加分享时出错！" . $id);
		}
		
		$admin_log->admin_insert_log_info($admin_id, $site_id, 6, "新增 首页美食分享id={$id}");
	}

}

try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/main_nav.php");
	
	check_cgi_pro();
	get_data();
	
	$g_show["error"] = 0;
	$g_show["errmsg"] = "";	
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_show;

	echo json_encode($g_show);
}

?>